package com.young.context;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public final class SelectorLocalizer {

    private static final Pattern PATTERN = Pattern.compile("_selector_\\.([a-z]+)\\.([a-z]+)\\.(\\S+)");

    /**
     * 如果不是selector属性，则直接保留
     * 如果是selector属性，则只保留符合当前profile和cloud部分
     *
     * @param properties
     * @param profile
     * @param cloud
     * @return
     */
    public static Map<String, Object> localize(Properties properties, String profile, String cloud) {
        Map<String, Object> container = new HashMap<>();

        //cloud,profile,value三级维度
        Map<String, Map<String, Map<String, Object>>> selector = new HashMap<>();
        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys) {
            if (!key.startsWith("_selector_")) {
                //如果不是selector，直接作为常量值
                container.put(key, properties.getProperty(key));
                continue;
            }

            Matcher matcher = PATTERN.matcher(key);
            if (!matcher.find() || matcher.groupCount() != 3) {
                continue;
            }
            String property = matcher.group(3);
            if (StringUtils.isEmpty(property)) {
                continue;
            }

            Map<String, Map<String, Object>> pm = selector.getOrDefault(property, new HashMap<>());
            selector.putIfAbsent(property, pm);

            String _cloud = matcher.group(2);
            Map<String, Object> cm = pm.getOrDefault(_cloud, new HashMap<>());
            pm.putIfAbsent(_cloud, cm);

            String _profile = matcher.group(1);
            cm.put(_profile, properties.getProperty(key));
        }
        container.putAll(selectorPropertiesNative(selector, profile, cloud));
        return container;
    }

    /**
     * 不支持自定义profile
     *
     * @param selector
     * @param profile
     * @param cloud
     * @return
     */
    private static Map<String, Object> selectorPropertiesNative(Map<String, Map<String, Map<String, Object>>> selector, String profile, String cloud) {
        Map<String, Object> container = new HashMap<>();
        Set<String> keys = selector.keySet();
        for (String key : keys) {
            Map<String, Map<String, Object>> mapper = selector.get(key);
            Map<String, Object> cm = mapper.get(cloud);
            if (cm != null && cm.containsKey(profile)) {
                container.put(key, cm.get(profile));
                continue;
            }

            cm = mapper.get(Cloud.DEFAULT.code);
            if (cm != null) {
                String _profile = cm.containsKey(profile) ? profile : Profile.DEFAULT.getStandard();
                container.put(key, cm.get(_profile));
                continue;
            }

            container.put(key, null);

        }
        return container;
    }
}
